﻿@model SimplCommerce.Module.PaymentCashfree.Areas.PaymentCashfree.ViewModels.CashfreeCheckoutForm
<label>Cashfree</label>

<div class="form-group">
    <div class="col-md-12">
        <button type="button" id="cashfree-payment-button" class="btn btn-order">Pay with Cashfree</button>
    </div>
</div>


<!-- Place the below div on your html -->
<div id="payment-div"></div>

<!-- Paste below code base before the closing tag(</body>) of body element -->
<script src="https://www.cashfree.com/assets/cashfree.sdk.v1.2.js" type="text/javascript"></script>
<script type="text/javascript">
    (function () {
        var data = {};
        data.orderId = "@Model.OrderId";
        data.orderAmount = "@Model.OrderAmount";
        data.customerName = "@Model.CustomerName";
        data.customerPhone = "@Model.CustomerPhone";
        data.customerEmail = "@Model.CustomerEmail";
        data.returnUrl = "@Model.ReturnURL";
        data.notifyUrl = "@Model.NotifyURL";
        data.appId = "@Model.AppId";
        data.paymentToken = "@Html.Raw(Model.PaymentToken)";
        var callback = function (event) {
            var eventName = event.name;
            console.log(event);
            switch (eventName) {
                case "PAYMENT_RESPONSE":
                    var request = $.ajax({
                        type: 'POST',
                        url: '@Url.Action("Charge", "Cashfree", new { Area = "PaymentCashfree" })',
                        data: event.response
                    }).done(function (data) {
                        window.location = "/checkout/success?orderId="+ data.orderId;
                     })
                    .fail(function (errors) {
                        console.log(errors.responseText);
                    });                 
                    break;
                case "PAYMENT_REQUEST":
                    console.log(event);
                    break;
                default:
                    console.log(event);
            };
        }
        var config = {};
        config.layout = { view: "popup", width: "650" };
        config.mode = "@Model.Mode";
        var response = CashFree.init(config);
        if (response.status == "OK") {
            cfInitialized = true;
        } else {
            //handle error
            console.log(response.message);
        }

        var button = document.querySelector('#cashfree-payment-button');
        button.addEventListener('click', function () {
            if (cfInitialized) {
                CashFree.makePayment(data, callback);
            }
        });
    })();
</script>
